using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;
using System.Collections;

namespace eHouse
{
    class LightsScheduleWindow:Window
    {
        private Window lightsschedulewindow;
        Canvas canvas = new Canvas();
        ArrayList light_rules;
        
        ArrayList rule_array = new ArrayList();
        ArrayList delete_array = new ArrayList();

        public LightsScheduleWindow(Window mw)
        {
            lightsschedulewindow = new Window();
            lightsschedulewindow.Height = SystemMetrics.ScreenHeight;
            lightsschedulewindow.Width = SystemMetrics.ScreenWidth;

            
            light_rules = LightsBL.GetInstance().GetRules();

            CreateWindow();       
        }

        private void CreateWindow()
        {
            if (light_rules.Count != 0)
            {   int i=0;
                
                foreach (Rule rule in light_rules)
                {
                    if (rule != null)
                    {
                        
                        string id = rule.ID.ToString().ToUpper();
                        string time = rule.Time;
                        bool[] states = rule.Body as bool[];
                        
                        bool state = states[4];
                        string on_off="";
                        if (state)
                            on_off = "ON";
                        else
                            on_off = "OFF";

                        rule_array.Add(new Text(Resources.GetFont(Resources.FontResources.NinaB), "Rule " + (i+1) + ": " + time + "-" + getRooms(states)+"-"+on_off));
                        delete_array.Add(new SimpleButton(new Image(Resources.GetBitmap(Resources.BitmapResources.delete)),30,30));

                        i++;
                    }
                }
                for (int j = 0; j < rule_array.Count; j++)
                {
                    if (rule_array[j] != null)
                    {
                        Canvas.SetTop((Text)rule_array[j], 0 + j * 30);
                        Canvas.SetLeft((Text)rule_array[j], 0);
                        canvas.Children.Add((Text)rule_array[j]);

                        Canvas.SetTop((SimpleButton)delete_array[j], 0 + j * 30);
                        Canvas.SetRight((SimpleButton)delete_array[j], 0);
                        canvas.Children.Add((SimpleButton)delete_array[j]);

                        ((SimpleButton)delete_array[j]).Click += new EventHandler(buttonDelete_Click);
                    }
                }
            }
            else
            {
                Text text_noRules = new Text(Resources.GetFont(Resources.FontResources.NinaB), "There are no light rules");
                Canvas.SetTop(text_noRules, 0);
                Canvas.SetLeft(text_noRules, 0);
                canvas.Children.Add(text_noRules);
            }

            
            Image image_newrule = new Image(Resources.GetBitmap(Resources.BitmapResources.edit));
            image_newrule.HorizontalAlignment = HorizontalAlignment.Center;
            image_newrule.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_newrule = new SimpleButton(image_newrule, lightsschedulewindow.Width / 2, lightsschedulewindow.Height / 5);
            button_newrule.BackColor = Colors.White;
            Canvas.SetBottom(button_newrule, 0);
            Canvas.SetLeft(button_newrule, 0);
            canvas.Children.Add(button_newrule);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, lightsschedulewindow.Width / 2, lightsschedulewindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            button_newrule.Click += new EventHandler(buttonNewRule_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);

            lightsschedulewindow.Child = canvas;
            lightsschedulewindow.Visibility = Visibility.Visible;
        }

        string getRooms(bool[] states)
        {
            string response = " ";
            string[] rooms = {"KITCHEN","LIVING ROOM", "BEDROOM","BATHROOM"};
            for (int i = 0; i < 4; i++)
            {
               
                    if (states[i])
                    {
                        response += rooms[i] + ",";
                    }
               
            }
            
            return response.Substring(0, response.Length - 1);
        }

        void buttonDelete_Click(object sender, EventArgs evt)
        {
            SimpleButton buttonPressed = (SimpleButton)sender;
            for (int i = 0; i < delete_array.Count; i++)
            {
                if (delete_array[i] == buttonPressed)
                {
                    Rule rule = light_rules[i] as Rule;
                    LightsBL.GetInstance().DeleteRule(rule.ID.ToUpper());
                    break;
                }
            }
            lightsschedulewindow.Close();
            new LightsScheduleWindow(this);

        }

        void buttonNewRule_Click(object sender, EventArgs evt)
        {
            AddLightRulesWindow alrw = new AddLightRulesWindow(this);
            lightsschedulewindow.Close();
        }
        
           void buttonReturn_Click(object sender, EventArgs evt)
        {
            lightsschedulewindow.Close();   
        }
    }
}
